﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace CC98.HtmlParser
{
	internal class ListPageParser : Parser<ListPageInfo>
	{

		private static void ParseBoards(string rawHtml, ListPageInfo info)
		{

			//				<a href="list.asp?boardid=570">
			//					<font color=#000066>防火视窗</font></a>主题：<a href="dispbbs.asp?Boardid=570&ID=3681481&replyID=559486741">山东德州公安局大楼突..</a><BR>作者：<a href="dispuser.asp?id=396972" target=_blank>qianjin1</a><BR>日期：<a>12/7/2011 10:33:42 PM&nbsp;</a>
			//			<font face=Arial>提高防火意识，建设和谐校园</font>版主：暂无</td>今日帖<font color=#FF0000>0</font>=主题 border=0  align=absmiddle>&nbsp;31				</td>
			//				<td width=45% valign=middle nowrap>
			//					<img src=pic/Forum_post.gif alt=文章 border=0 align=absmiddle>&nbsp;15372				</td>
			//				</tr>
			//			</table>
			//			</td>
			//		</tr>
			//	</table>	</td>
			//</tr>
			//</table>



			var pattern = @"<a href=""list\.asp\?boardid=(?<boardid>\d+)"">\s*<font color=\#000066>(?<boardname>.*?)</font></a>.*?主题：<a href=""dispbbs\.asp\?Boardid=\d+&ID=(?<topicid>\d+)&replyID=(?<replyid>\d+)"">(?<title>.*?)</a><BR>作者：<a href=""dispuser\.asp\?id=(?<userid>\d+)"" target=_blank>(?<username>.*?)</a><BR>日期：<.*?>(?<time>.*?)&nbsp;</a>.*?<font face=Arial><img .*?>(?<desc>.*?)</font>.*?版主：((<a .*?>(?<manager>.*?)</a>&nbsp;)*|暂无)</td>.*?今日帖.*?<font color=\#FF0000>(?<today>\d+)</font>.*?主题.*?&nbsp;(?<topic>\d+).*?文章.*?&nbsp;(?<post>\d+)";

			var ms = rawHtml.MatchAll(pattern);

			foreach (Match i in ms)
			{
				var item = new ListPageBoardInfo
				{
					Id = i.Groups["boardid"].Value.ToInt32(),
					Name = i.Groups["boardname"].Value,
					Descrption = i.Groups["desc"].Value,
					LastTopicId = i.Groups["topicid"].Value.ToInt32(),
					LastTopicReplyId = i.Groups["replyid"].Value.ToInt32(),
					LastTopicTitle = i.Groups["title"].Value,
					LastTopicReplyUser = i.Groups["username"].Value,
					LastTopicReplyTime = i.Groups["time"].Value.ToDateTime(),
					TopicToday = i.Groups["today"].Value.ToInt32(),
					TopicCount = i.Groups["topic"].Value.ToInt32(),
					PostCount = i.Groups["post"].Value.ToInt32()
				};

				foreach (Capture g in i.Groups["manager"].Captures)
				{
					item.Managers.Add(g.Value);
				}

				info.Boards.Add(item);
			}
		}

		private static void ParseTopics(string rawHtml, ListPageInfo info)
		{
			var pattern = @"<tr align=middle>.*?<a id="".*?"" href=""dispbbs\.asp\?boardID=\d+&ID=(?<topicid>\d+).*? title=""(?<title>.*?)<br>.*?发表于(?<time>.*?)<br>最后跟贴："">(?<content>.*?)</a>.*?<a href=dispuser\.asp\?id=(?<userid>\d+) target=_blank>(?<username>\S*?)</a></a>.*?<td width=\* nowrap class=tablebody1>\s+(?<reply>\d+)/(?<visit>\d+)\s+</td>.*?<a .*?>(?<replytime>.*?)</a>&nbsp;.*?lastPostInfo\('\d+','\{""usr"":""(?<replyuser>.*?)"",""txt"":""(?<replycontent>.*?)""\}'\);</script>";


			var ms = rawHtml.MatchAll(pattern);

			foreach (Match i in ms)
			{
				var item = new ListPageTopicInfo
				{
					Id = i.Groups["topicid"].Value.ToInt32(),
					Title = i.Groups["title"].Value,
					AuthorName = i.Groups["username"].Value,
					AuthorId = i.Groups["userid"].Value.ToInt32(),
					PostTime = i.Groups["time"].Value.ToDateTime(),
					ReplyCount = i.Groups["reply"].Value.ToInt32(),
					VisitCount = i.Groups["visit"].Value.ToInt32(),
					LastUpdateUser = i.Groups["replyuser"].Value,
					LastReplyContent = i.Groups["replycontent"].Value,
					LastUpdateTime = i.Groups["replytime"].Value.ToDateTime()
				};

				info.Topics.Add(item);
			}
		}

		public override ListPageInfo Parse(string rawHtml)
		{
			var result = new ListPageInfo();

			ParseBoards(rawHtml, result);
			ParseTopics(rawHtml, result);

			return result;
		}
	}
}
